﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WorldView
{
    public partial class NpcWindow : Form
    {
        Varies v;
        public bool Changed = false;
        public NpcWindow(Varies v1)
        {
            v = v1;
            InitializeComponent();
            if (v.npc.Count < 1)
            {
                v.npc.Add(new Npc("Guide", 0, 0, false, 0, 0));
            }
            for (int i = 0; i < v.npc.Count; i++)
            {
                NpcList.Items.Add(v.npc[i].name);
            }
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            v.npc.Add(new Npc("New", 0, 0, false, 0, 0));
            NpcList.Items.Add("New");
        }

        private void RemoveButton_Click(object sender, EventArgs e)
        {
            v.npc.Remove(v.npc[NpcList.SelectedIndex]);
            NpcList.Items.Remove(NpcList.SelectedItem);
            if (NpcList.Items.Count > 0)
            {
                Changed = true;
                NpcList.SelectedIndex = 0;

            }
            else
            {
                XLoc.Enabled = false;
                YLoc.Enabled = false;
                NpcNameList.Enabled = false;
                hasHome.Enabled = false;
                HomeX.Enabled = false;
                HomeY.Enabled = false;
            }
        }

        private void NpcList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Changed)
            {
                NpcNameList.Text = v.npc[NpcList.SelectedIndex].name;
                XLoc.Text = v.npc[NpcList.SelectedIndex].X.ToString();
                YLoc.Text = v.npc[NpcList.SelectedIndex].Y.ToString();
                hasHome.Checked = v.npc[NpcList.SelectedIndex].hasHome;
                HomeX.Text = v.npc[NpcList.SelectedIndex].HomeX.ToString();
                HomeY.Text = v.npc[NpcList.SelectedIndex].HomeY.ToString();

                XLoc.Enabled = true;
                YLoc.Enabled = true;
                NpcNameList.Enabled = true;
                hasHome.Enabled = true;
                HomeX.Enabled = true;
                HomeY.Enabled = true;
            }
        }

        private void NpcNameList_SelectedIndexChanged(object sender, EventArgs e)
        {
            v.npc[NpcList.SelectedIndex].name = NpcNameList.Text;
            NpcList.Items[NpcList.SelectedIndex] = NpcNameList.Text;
            Changed = false;
        }

        private void XLoc_TextChanged(object sender, EventArgs e)
        {
            v.npc[NpcList.SelectedIndex].X = Convert.ToInt32(XLoc.Text);
        }

        private void YLoc_TextChanged(object sender, EventArgs e)
        {
            v.npc[NpcList.SelectedIndex].Y = Convert.ToInt32(YLoc.Text);
        }

        private void hasHome_CheckedChanged(object sender, EventArgs e)
        {
            v.npc[NpcList.SelectedIndex].hasHome = hasHome.Checked;
        }

        private void HomeX_TextChanged(object sender, EventArgs e)
        {
            v.npc[NpcList.SelectedIndex].HomeX = Convert.ToInt32(HomeX.Text);
        }

        private void HomeY_TextChanged(object sender, EventArgs e)
        {
            v.npc[NpcList.SelectedIndex].HomeY = Convert.ToInt32(HomeY.Text);
        }

        private void NpcList_MouseClick(object sender, MouseEventArgs e)
        {
            NpcNameList.Text = v.npc[NpcList.SelectedIndex].name;
            XLoc.Text = v.npc[NpcList.SelectedIndex].X.ToString();
            YLoc.Text = v.npc[NpcList.SelectedIndex].Y.ToString();
            hasHome.Checked = v.npc[NpcList.SelectedIndex].hasHome;
            HomeX.Text = v.npc[NpcList.SelectedIndex].HomeX.ToString();
            HomeY.Text = v.npc[NpcList.SelectedIndex].HomeY.ToString();

            XLoc.Enabled = true;
            YLoc.Enabled = true;
            NpcNameList.Enabled = true;
            hasHome.Enabled = true;
            HomeX.Enabled = true;
            HomeY.Enabled = true;
        }

        private void NpcList_KeyDown(object sender, KeyEventArgs e)
        {
            NpcNameList.Text = v.npc[NpcList.SelectedIndex].name;
            XLoc.Text = v.npc[NpcList.SelectedIndex].X.ToString();
            YLoc.Text = v.npc[NpcList.SelectedIndex].Y.ToString();
            hasHome.Checked = v.npc[NpcList.SelectedIndex].hasHome;
            HomeX.Text = v.npc[NpcList.SelectedIndex].HomeX.ToString();
            HomeY.Text = v.npc[NpcList.SelectedIndex].HomeY.ToString();

            XLoc.Enabled = true;
            YLoc.Enabled = true;
            NpcNameList.Enabled = true;
            hasHome.Enabled = true;
            HomeX.Enabled = true;
            HomeY.Enabled = true;
        }

        private void NpcList_KeyUp(object sender, KeyEventArgs e)
        {
            NpcNameList.Text = v.npc[NpcList.SelectedIndex].name;
            XLoc.Text = v.npc[NpcList.SelectedIndex].X.ToString();
            YLoc.Text = v.npc[NpcList.SelectedIndex].Y.ToString();
            hasHome.Checked = v.npc[NpcList.SelectedIndex].hasHome;
            HomeX.Text = v.npc[NpcList.SelectedIndex].HomeX.ToString();
            HomeY.Text = v.npc[NpcList.SelectedIndex].HomeY.ToString();

            XLoc.Enabled = true;
            YLoc.Enabled = true;
            NpcNameList.Enabled = true;
            hasHome.Enabled = true;
            HomeX.Enabled = true;
            HomeY.Enabled = true;
        }

        private void ExportButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialogW = new SaveFileDialog();
            dialogW.Filter = "Terraria Npc File (*.npc)|*.npc";
            dialogW.Title = "Select Npc File";
            dialogW.FileName = string.Format("{0}.npc", System.IO.Path.GetFileNameWithoutExtension(""));
            string filePath = (dialogW.ShowDialog() == DialogResult.OK) ? dialogW.FileName : string.Empty;
            if (filePath == string.Empty)
            {
                return;
            }
            else if (new System.IO.FileInfo(filePath).Extension.ToUpper() != ".NPC")
            {
                MessageBox.Show("Output File MUST End in .npc", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            using (FileStream stream = new FileStream(filePath, FileMode.Create))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    for (int i = 0; i < v.npc.Count; i++)
                    {
                        writer.Write(true);
                        writer.Write(v.npc[i].name);
                        writer.Write(v.npc[i].X);
                        writer.Write(v.npc[i].Y);
                        writer.Write(v.npc[i].hasHome);
                        writer.Write(v.npc[i].HomeX);
                        writer.Write(v.npc[i].HomeY);
                    }
                    writer.Close();
                }
                stream.Close();

            }
        }

        private void ImportButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialogW = new OpenFileDialog();
            dialogW.Filter = "Terraria Npc File (*.npc)|*.npc";
            dialogW.Title = "Select Npc File";
            dialogW.FileName = string.Format("{0}.npc", System.IO.Path.GetFileNameWithoutExtension(""));
            string filePath = (dialogW.ShowDialog() == DialogResult.OK) ? dialogW.FileName : string.Empty;
            if (filePath == string.Empty)
            {
                return;
            }
            else if (new System.IO.FileInfo(filePath).Extension.ToUpper() != ".NPC")
            {
                MessageBox.Show("Output File MUST End in .npc", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            v.npc.Clear();
            using (FileStream stream = new FileStream(filePath, FileMode.Open))
            {
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    bool active = reader.ReadBoolean();
                    int i = 0;
                    while (active)
                    {

                        v.npc.Add(new Npc(reader.ReadString(),reader.ReadSingle(),reader.ReadSingle(),reader.ReadBoolean(),reader.ReadInt32(),reader.ReadInt32()));
                        try
                        {
                            active = reader.ReadBoolean();
                        }
                        catch (EndOfStreamException)
                        {
                            active = false;
                        }
                        i++;


                    }
                    reader.Close();
                }
                stream.Close();
            }
            NpcList.Items.Clear();
            for (int i = 0; i < v.npc.Count; i++)
            {
                NpcList.Items.Add(v.npc[i].name);
            }
        }
    }
}
